<template>
    <div>
         <h3>门店新增</h3>
         <table class="table">
         <thead>
            <tr>
                <td>门店名称</td>
                <td><input type="text" v-model="obj.storeName"></td>
            </tr>
            <tr>
                <td>所在地区</td>
                <td><select v-model="obj.areaId">
            <option value="0">请选择地区</option>
            <option v-for="a in binddata" :value="a.addressId">{{a.addressName}}</option>
            </select></td>
            </tr>
            <tr>
                <td>门店店长名</td>
                <td><input type="text" v-model="obj.manager"></td>
            </tr>
            <tr>
                <td>店长手机号</td>
                <td><input type="text" v-model="obj.phone"></td>
            </tr>
            <tr>
                <td>门店成立日期</td>
                <td><input type="date" v-model="obj.createDate"></td>
            </tr>
            <tr>
                <td>同时接待最大人数</td>
                <td><input type="number" v-model="obj.maxPeople"></td>
            </tr>
            <tr>
                <td>状态</td>
                <td>
                    <input type="radio" v-model="obj.start" name="start" value="1">休息
                    <input type="radio" v-model="obj.start" name="start" value="2">营业
                    <input type="radio" v-model="obj.start" name="start" value="3">关闭
                </td>
            </tr>
            <tr>
                <td colspan="2"><input type="button" value="新增" @click="addStore"></td>
            </tr>
         </thead>
         </table>
    </div>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'

const router=useRouter()

onMounted(()=>{
    BindAddr()
})

const obj=ref({
  storeName:'',
  manager:'',
  phone:'',
  createDate:'',
  maxPeople: 0,
  start: 0,
  areaId: 0
})

const addStore=()=>{
    if(obj.value.storeName=='')
{
    alert('门店名不能为空')
    return;
}
if(obj.value.areaId==0)
{
    alert('地区不能为空')
    return;
}
if(obj.value.manager=='')
{
    alert('店长名不能为空')
    return;
}
if(obj.value.phone=='')
{
    alert('手机号不能为空')
    return;
}
let tel=/^1[3-9]\d{9}$/
if(!tel.test(obj.value.phone))
{
     alert('手机号格式错误')
    return;
}
if(obj.value.createDate=='')
{
    alert('成立日期不能为空')
    return;
}
if(obj.value.maxPeople==0)
{
    alert('同时接待最大人数不能为空')
    return;
}
if(obj.value.start==0)
{
    alert('状态不能为空')
    return;
}
    axios.post("/api/Store/AddStore",obj.value).then(res=>{
        if(res.data>0)
    {
        alert('新增成功')
        router.push("/");
    }
       else if(res.data==-2) 
       {
        alert('名称不能重复!!!!')
        return;
       }
       else
       {
        alert('新增失败')
        return;
       }
    })
}
//绑定地区下拉框
const binddata=ref([])
const BindAddr=()=>{
    axios.get("/api/Address/GetAllAddress",{}).then(res=>{
        binddata.value=res.data
    })
}

</script>

<style scoped>

</style>